import {
	sendSms as sendSmsApi
} from '@/api/user.js'
import {
	showToast
} from 'vant';
import {
	ref,
	computed
} from 'vue';


export default function(userForms, type) {

	let tipMsg = ref('发送验证')
	let time = 10;
	let timer = null; // 定时器
	let isPending = false; // 是否可用

	function sendSms() {
		// 		console.log(userForms) 
		// console.log(userForms.username)

		if (!/^1[3-9]\d{9}$/.test(userForms.username)) {

			showToast('手机号错误')
			return
		}
		if (isPending) {
			showToast('请等待倒计时结束')
			return
		}

		isPending = true;

		tipMsg.value = '发送中...'

		sendSmsApi({
			"mobile": userForms.username,
			"type": type
		}).then(res => {
			if (res.code == 0) {

				timer = setInterval(v => {
					tipMsg.value = time + 's后重新获取'
					time--

					if (time <= 0) {
						time = 10;
						clearInterval(timer)
						isPending = false
						tipMsg.value = '再次获取'
					}

				}, 1000)

			} else {

				isPending = false
				tipMsg.value = '再次获取'
			}

		})



	}

	return {
		sendSms,
		tipMsg
	}


}